home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / a86v400.zip / A09.DOC < prev    next >
Text File  |  1994-12-21  |  26KB  |  608 lines

  1. CHAPTER 9   DIRECTIVES IN A86
  2.  
  3.  
  4. Segments in A86
  5.  
  6. The following discussion applies when A86 is assembling a .COM
  7. See the next chapter for the discussion of segmentation for .OBJ
  8. files.
  9.  
  10. A86 views the 86 computer's memory space as having two parts: The
  11. first part is the program, whose contents are the object bytes
  12. generated by A86 during its assembly of the source.   A86 calls
  13. this area the CODE SEGMENT.  The second part is the data area,
  14. whose contents are generated by the program after it starts
  15. running.  A86 calls this area the DATA SEGMENT.
  16.  
  17. Please note well that the only difference between the CODE and
  18. DATA segments is whether the contents are generated by the
  19. program or the assembler.  The names CODE and DATA suggest that
  20. program code is placed in the CODE segment, and data structures
  21. go in the DATA segment.  This is mostly true, but there are
  22. exceptions.  For example, there are many data structures whose
  23. contents are determined by the assembler: pointer tables, arrays
  24. of pre-defined constants, etc.  These tables are assembled in the
  25. CODE segment.
  26.  
  27. In general, you will want to begin your program with the
  28. directive DATA SEGMENT, followed by all your program variables
  29. and uninitialized data structures, using the directives DB, DW,
  30. and STRUC.  If you do not give an ORG directive, A86 will begin
  31. the allocation immediately following the end of the .COM program.
  32. You can end the DATA SEGMENT allocation lines with the DATA ENDS
  33. directive, followed by the program code itself.  A short program
  34. illustrating this suggested usage follows:
  35.  
  36. DATA SEGMENT
  37.   ANSWER_BYTE  DB ?
  38.   CALL_COUNT   DW ?
  39.  
  40. CODE SEGMENT
  41.   JMP MAIN
  42.  
  43. TRAN_TABLE:
  44.   DB 16,3,56,23,0,9,12,7
  45.  
  46. MAIN:
  47.   MOV BX,TRAN_TABLE
  48.   XLATB
  49.   MOV ANSWER_BYTE,AL
  50.   INC CALL_COUNT
  51.   RET
  52.  
  53. A86 allows you to intersperse CODE SEGMENTs and DATA SEGMENTs
  54. throughout your program; but in general it is best to put all
  55. your DATA SEGMENT declarations at the top of your program, to
  56. avoid problems with forward referencing.
  57.                                                               9-2
  58.  
  59. CODE ENDS and DATA ENDS Statements
  60.  
  61. For compatibility with Intel/IBM assemblers, A86 provides the
  62. CODE ENDS and DATA ENDS statements.  The CODE ENDS statement is
  63. ignored; we assume that you have not nested a CODE segment inside
  64. a DATA segment.  The DATA ENDS statement is equivalent to a CODE
  65. SEGMENT statement.
  66.  
  67.  
  68.  
  69. The ORG Directive
  70.  
  71. Syntax:  ORG address
  72.  
  73. ORG moves the output pointer (the location counter at which
  74. assembly is currently taking place within the current segment) to
  75. the value of the operand.  In the CODE segment, the operand
  76. should be an absolute constant, or an expression evaluating to an
  77. absolute, non-forward-referenced constant.  In the DATA segment,
  78. the operand may be a forward reference or an expression
  79. containing one or more forward references.  All symbols in the
  80. segment will be resolved when the forward references to the ORG
  81. operand are all resolved.
  82.  
  83. There is a special side effect to ORG when it is used in the CODE
  84. segment.  If you begin your code segment with ORG 0, then A86
  85. knows that you are not assembling a .COM program; but are instead
  86. assembling a code segment to be used in some other context
  87. (examples: programming a ROM, or assembling a procedure for older
  88. versions of Turbo Pascal).  The output file will start at 0, not
  89. 0100 as in a .COM file; and the default extension for the output
  90. file will be .BIN, not .COM.  However, if you later issue an ORG
  91. 0100 directive, the default will revert back to .COM.
  92.  
  93. Other than in the above example, you should not in general issue
  94. an ORG within the CODE segment that would lower the value of the
  95. output pointer.  This is because you thereby put yourself in
  96. danger of losing part of your assembled program.  If you
  97. re-assemble over space you have already assembled, you will
  98. clobber the previously-assembled code.  Also, be aware that the
  99. size of the output program file is determined by the value of the
  100. code segment output pointer when the program stops.  If you ORG
  101. to a lower value at the end of your program, the output program
  102. file will be truncated to the lower-value address.
  103.  
  104. Again, almost no program producing a .COM file will need any ORG
  105. directive in the code segment.  There is an implied ORG 0100 at
  106. the start of the program.  You just start coding instructions,
  107. and the assembler will put them in the right place.
  108.                                                               9-3
  109.  
  110. The EVEN Directive
  111.  
  112. Syntax:  EVEN  constant
  113.  
  114. The EVEN directive coerces the current output pointer to a value
  115. which is an exact multiple of the operand.  If no operand is
  116. given, a value of 2 is assumed.  In a DATA SEGMENT or STRUC, it
  117. does so by adding to the current output pointer if necessary.  In
  118. a code segment, it outputs an appropriate number of NOP
  119. instruction bytes.  EVEN is most often used in data segments,
  120. before a sequence of DW directives.  Machines beyond the original
  121. 8088 fetch words more quickly when they are aligned onto even
  122. addresses; so the EVEN directive insures that your program will
  123. have the faster access to those DW's that follow it.  Also useful
  124. are EVEN 4 for doubleword alignment, and EVEN 16 for paragraph
  125. alignment.  Be aware, though, that if you use the EVEN directive
  126. in .OBJ mode, the containing SEGMENT directive should have an
  127. alignment type at least as great as your EVEN operand, to achieve
  128. the desired alignment at its final memory location.
  129.  
  130.  
  131. Data Allocation Using DB, DW, DD, DQ, and DT
  132.  
  133. The 86 computer family supports the three fundamental data types
  134. BYTE, WORD, and DWORD.  A byte is eight bits, a word is 16 bits
  135. (2 bytes), and a doubleword is 32 bits (4 bytes).  In addition,
  136. the 87 floating point processor manipulates 8-byte quantities,
  137. which we call Q-words, and 10-byte quantities, which we call
  138. T-bytes.  The A86 data allocation statement is used to specify
  139. the bytes, words, doublewords, Q-words, and T-bytes which your
  140. program will use as data.  The syntax for the data allocation
  141. statement is as follows:
  142.  
  143. (optional var-name)  DB  (list of values)
  144. (optional var-name)  DW  (list of values)
  145. (optional var-name)  DD  (list of values)
  146. (optional var-name)  DQ  (list of values)
  147. (optional var-name)  DT  (list of values)
  148.  
  149. The variable name, if present, causes that name to be entered
  150. into the symbol table as a memory variable with type BYTE (for
  151. DB), WORD (for DW), DWORD (for DD), QWORD (for DQ), or TBYTE (for
  152. DT). The variable name should NOT have a colon after it, unless
  153. you wish the name to be a label (instructions referring to it
  154. will interpret the label as the constant pointer to the memory
  155. location, not its contents).
  156.  
  157. The DB statement is used to reserve bytes of storage; DW is used
  158. to reserve words.  The list of values to the right of the DB or
  159. DW serves two purposes.  It specifies how many bytes or words are
  160. allocated by the statement, as well as what their initial values
  161. should be.  The list of values may contain a single value or more
  162. than one, separated by commas.  The list can even be missing;
  163. meaning that we wish to define a byte or word variable at the
  164. same location as the next variable.
  165.                                                               9-4
  166.  
  167. If the data initialization is in the DATA segment, the values
  168. given are ignored, except as place markers to reserve the
  169. appropriate number of units of storage.  The use of "?", which in
  170. .COM mode is a synonym for zero, is recommended in this context
  171. to emphasize the lack of actual memory initialization. When A86
  172. is assembling .OBJ files, the ?-initialization will cause a break
  173. in the segment (unless ? is embedded in a nested DUP containing
  174. non-? terms, in which case it is a synonym for zero).
  175.  
  176. A special value which can be used in data initializations is the
  177. DUP construct, which allows the allocation and/or initialization
  178. of blocks of data.  The expression  n DUP x  is equivalent to a
  179. list with x repeated n times.  "x" can be either a single value,
  180. a list of values, or another DUP construct nested inside the
  181. first one.  The nested D